//
//  AdBussinessController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 2017/1/3.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "AdBussinessController.h"
#import "BusinessModel.h"
#import "BusinessViewCell.h"
#import "BusinessDetailsController.h"

@interface AdBussinessController ()<UITableViewDelegate,UITableViewDataSource>
{
    TopPullRefreshAnaimation *gifHeader;
    MBProgressHUD *progressHUD;
    NSUInteger refreshIndex;
    UIImageView *noDrawImage;
    
}


@property(nonatomic,strong)UITableView *businessView;

@property(nonatomic,strong)NSMutableArray *businessArray;

@end

@implementation AdBussinessController

static NSString *const businessIdentifier = @"AdBussinessControllerCell";



- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [progressHUD hide:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addTableView];
    //上拉刷新
    [self addPullUpdateData];
    
    
    //数据加载
    [self BusinessDetailsLoadDataCity];
    //
    refreshIndex = 1;
    
}


- (void)addTableView
{
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    UITableView *businessView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) style:UITableViewStyleGrouped];
    self.businessView = businessView;
    [self.view addSubview:businessView];
    self.businessView.rowHeight = HEIGHT/5;
    businessView.backgroundColor = [UIColor whiteColor];
    businessView.delegate = self;
    businessView.dataSource = self;
}



#pragma mark >>>>>>>>刷新
- (void)addPullUpdateData
{
    self.businessView.mj_footer = [TopPullRefreshAnaimation footerWithRefreshingTarget:self refreshingAction:@selector(BusinessDetailsLoadMoreData)];
    
}

- (void)BusinessDetailsLoadMoreData
{
    refreshIndex ++;
    [self BusinessDetailsLoadDataCity];
    [self.businessView.mj_footer endRefreshing];
}

- (void)BusinessDetailsLoadDataCity{

    
    //菊花加载
    progressHUD = [MBProgressHUD loadingInformationToView];
    [self.view addSubview:progressHUD];
    
    
    [noDrawImage removeFromSuperview];
    NSString *P = [NSString stringWithFormat:@"%ld",refreshIndex];
    
    NSDictionary *params = @{@"p":P,
                             };

    NSString *str = [NSString stringWithFormat:@"/api/Business/businessList%@",self.value];
    
    NSString *urlString = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:urlString completeBlock:^(id object) {
       
        NSLog(@"%@",object);
        NSArray *arr = object[@"data"];
        if (JudgeBackDataCode){
            for (NSDictionary *dict in arr) {
                BusinessModel *businessModel = [[BusinessModel alloc] initWithDictionary:dict error:nil];
                
                if (businessModel) {
                    [self.businessArray addObject:businessModel];
                }
            }
            
        }else{
            
            [self.businessView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.businessView reloadData];
        [progressHUD hide:YES];
        
        
        
    }];
    
    
}




#pragma mark>>>>>>>>>>>数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.businessArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    BusinessViewCell *cell = [tableView dequeueReusableCellWithIdentifier:businessIdentifier];
    
    if (cell==nil) {
        
        cell = [[BusinessViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:businessIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    BusinessModel *businessModel = self.businessArray[indexPath.row];
    [cell.shopsImg sd_setImageWithURL:[NSURL URLWithString:businessModel.sdpic] placeholderImage:[UIImage imageNamed:@"businessNopict.jpg"]];
    cell.shopsNameLabel.text = businessModel.title;
    cell.shopsDetailsLabel.text = businessModel.zycp;
    [cell.shopsAddress setTitle:businessModel.county forState:UIControlStateNormal];
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

#pragma mark>>>>>>>>>>>>代理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BusinessDetailsController *detailVC = [[BusinessDetailsController alloc] init];
    BusinessModel *businessModel = self.businessArray[indexPath.row];
    detailVC.bussinessId = businessModel.replaceID;
    [self.navigationController pushForm:self.navigationController pushToAndHidden:detailVC];
}

- (NSMutableArray *)businessArray {
    if (!_businessArray) {
        _businessArray = [NSMutableArray array];
    }
    
    return _businessArray;
}

@end
